# -*-coding:utf-8-*-

__author__ = 'nickleo'

import requests
from config import HogWartsConfig as cfg


def get_clusterlist(name_server):
    url = cfg().get_global_config('url') + "/cluster/list/"
    tmp_url = url + name_server
    return http_client(tmp_url)


def get_topiclist(name_server):
    url = cfg().get_global_config('url') + "/topic/list/"
    tmp_url = url + name_server
    return http_client(tmp_url)


def get_topicroute(name_server, topic):
    url = cfg().get_global_config('url') + "/topic/route/"
    tmp_url = url + name_server + '/' + topic
    return http_client(tmp_url)


def get_brokerTotal(_clustername, _bid, _startTime, _endTime):
    url = cfg().get_global_config('url') + "/data/total/" + _clustername + "/" + _bid
    tmp_url = url + '?dateBegin=' + _startTime + '&dateEnd=' + _endTime
    return http_client(tmp_url)


def get_brokerDetailTotal(_clustername, _bid, _startTime, _endTime):
    url = cfg().get_global_config('url') + "/data/total/details/" + _clustername + "/" + _bid
    tmp_url = url + '?dateBegin=' + _startTime + '&dateEnd=' + _endTime
    return http_client(tmp_url)


def get_brokerTps(_clustername, _bid, _startTime, _endTime):
    url = cfg().get_global_config('url') + "/data/tps/" + _clustername + "/" + _bid
    tmp_url = url + '?dateBegin=' + _startTime + '&dateEnd=' + _endTime
    return http_client(tmp_url)


def get_progress_data_one(name_server, group):
    url = cfg().get_global_config('url') + "/consumer/progress/"
    tmp_url = url + name_server + '/' + group
    return http_client(tmp_url)


# 1、更新broker配置
# /broker/config/update/{namesrvaddr}?clustername=XXX&&key=XXX&&value=XXX
# 返回成功为success字符串，失败空字符串
def up_broker_cfg(namesrvaddr, clustername, key, value):
    url = cfg().get_global_config('url') + "/broker/config/update/" + namesrvaddr
    tmp_url = url + '/' + clustername + '/' + key + '/' + value
    return http_client(tmp_url)

# /config/update/{namesrvaddr}/{brokeraddr}/{key}/{value}
def up_broker_cfg_addr(namesrvaddr, brokeraddr, key, value):
    url = cfg().get_global_config('url') + "/config/update/" + namesrvaddr
    tmp_url = url + '/' + brokeraddr + '/' + key + '/' + value
    return http_client(tmp_url)




# 2、添加订阅组信息
# /consumer/addsubgroup/{namesrvaddr}/{clustername}/{groupname}
# 返回成功信息的json，失败返回空

def add_sub_group(namesrvaddr, clustername, groupname):
    url = cfg().get_global_config('url') + "/consumer/addsubgroup/" + namesrvaddr
    tmp_url = url + '/' + clustername + '/' + groupname
    return http_client(tmp_url)


# 3、删除订阅组
# / consumer / deletesubgroup / {namesrvaddr} / {clustername} / {groupname}
# 返回成功为success字符串，失败空字符串
def del_sub_group(namesrvaddr, clustername, groupname):
    url = cfg().get_global_config('url') + "/consumer/deletesubgroup/" + namesrvaddr
    tmp_url = url + '/' + clustername + '/' + groupname
    return http_client(tmp_url)


# 4、创建Topic
# / topic / create / {namesrvaddr} / {clustername} / {topicname}
# 返回成功信息的json，失败返回空
def add_topic(namesrvaddr, clustername, topicname):
    url = cfg().get_global_config('url') + "/topic/create/" + namesrvaddr
    tmp_url = url + '/' + clustername + '/' + topicname
    return http_client(tmp_url)


# 5、删除Topic
# / topic / delete / {namesrvaddr} / {clustername} / {topicname}
# 返回成功为success字符串，失败空字符串
def del_topic(namesrvaddr, clustername, topicname):
    url = cfg().get_global_config('url') + "/topic/delete/" + namesrvaddr
    tmp_url = url + '/' + clustername + '/' + topicname
    return http_client(tmp_url)


# 6、查看Consumer连接
# / connection / consumer / {namesrvaddr} / {group}
# 返回成功信息的json，失败返回空

def get_consumer_connection(namesrvaddr, group):
    url = cfg().get_global_config('url') + "/connection/consumer/" + namesrvaddr
    tmp_url = url + '/' + group
    return http_client(tmp_url)


# 7、MsgId查询消息
# / message / id / {namesrvaddr} / {msgid}
# 查到返回json，无返回空
def get_message_id(namesrvaddr, msgid):
    url = cfg().get_global_config('url') + "/message/id/" + namesrvaddr
    tmp_url = url + '/' + msgid
    return http_client(tmp_url)


# 8、key查询消息
# / message / key / {namesrvaddr} / {key} / {topic}
# 查询到得消息可能是多条，json，无返回空

def get_message_key(namesrvaddr, key, topic):
    url = cfg().get_global_config('url') + "/message/key/" + namesrvaddr
    tmp_url = url + '/' + key + '/' + topic
    return http_client(tmp_url)


# 9、offset查询消息
# / message / offset / {namesrvaddr} / {brokername} / {topic} / {queueid} / {offset}
# 查到返回json，无返回空

def get_message_offset(namesrvaddr, brokername, topic, queueid, offset):
    url = cfg().get_global_config('url') + "/message/offset/" + namesrvaddr
    tmp_url = url + '/' + brokername + '/' + topic + '/' + queueid + '/' + offset
    return http_client(tmp_url)


def http_client(_url):
    print "_url: " + _url
    import traceback

    try:
        return requests.get(_url).text
    except:
        print 'http get catch ex '
        return None
        print traceback.format_exc()




